import { PropsWithChildren, useMemo } from "react";

type TCondition = () => boolean;
type TConditionPromise = () => Promise<boolean>;

interface IConditionProps {
  className?: string;
  condition: boolean | TCondition | TConditionPromise;
}
export function Condition(props: PropsWithChildren<IConditionProps>) {
  const { condition } = props;

  const result = useMemo(() => {
    if (typeof condition === "boolean") {
      return condition;
    }

    if (typeof condition === "function") {
      return condition();
    }

    return false;
  }, [condition]);

  return result ? props.children : null;
}
